package praktikumid.w09.p4;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * Panel with buttons in it.
 * @author Ago
 *
 */
public class ButtonPanel extends JPanel {
	/**
	 * Reference to the main frame. Helps us to
	 * write less code.
	 * Note, that it doesn't duplicate frame
	 * in memory. So, having a reference to an
	 * existing object uses only memory for the
	 * reference itself (32 or 64 bits).
	 */
	MyFrame frame;
	
	/**
	 * Inner listener class for the "draw rect" button.
	 * @author Ago
	 *
	 */
	class DrawRectListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// called when action is performed
			// (in our case, button is clicked)
			
			// because of the frame reference,
			// you can see that the implementation
			// of the actionPerformed method
			// is shorter than in "draw oval"
			// listener.
			frame.getDrawPanel().drawRect(200, 200);
		}
		
	}
	/**
	 * Constructor, which initializes the panel.
	 * It also takes in an argument which points
	 * to the frame (to shorten our code in
	 * listeners).
	 * @param f
	 */
	public ButtonPanel(MyFrame f) {
		frame = f;
		setBackground(Color.ORANGE);
		
		setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		JButton drawOval = new JButton("draw oval");
		JButton drawRect = new JButton("draw rect");
		JButton drawLine = new JButton("draw line");
		
		// button needs a listener in order to act
		// on click.
		// First one uses a separate class in a separate file.
		drawOval.addActionListener(new DrawOvalListener());
		// Example of inner class usage.
		drawRect.addActionListener(new DrawRectListener());
		// Here we use anonymous class -
		// we declare and instantiate it at the same time.
		// So, anonymous class doesn't have name
		drawLine.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.getDrawPanel().drawLine(300, 300);
			}
		});
		
		add(drawOval);
		add(drawRect);
		add(drawLine);
	}
}
